module.exports = {
    // 添加题目
    add: async (ctx, next) => {
        const {description, title, type, categoryName, categoryId,options,answers } = ctx.request.body
        console.log(description, title, type, categoryName, categoryId,options,answers );
        
        try {
            let [res] = await ctx.db.query('insert into questions(description,title, type, categoryName, categoryId,options,answers,createTime) values(?,?,?,?,?,?,?,?)', [description,title, type, categoryName, categoryId,JSON.stringify(options),answers, new Date()])
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '添加题目成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '创建失败' + error
            }
        }
    },
    // 修改题题目
    update: async (ctx, next) => {
        const {id, description,title,options,answers} = ctx.request.body
        try {
           let [res] = await ctx.db.query('update questions set description=?, title=? ,options=?,answers=?,updateTime=? where id=?',[description,title,options,answers,new Date(),id])
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '更新成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '更新失败' + error
            }
        }
    },
    // 删除题目
    del: async (ctx, next) => {
        const { id } = ctx.request.body
        try {
           let [res] = await ctx.db.query('update questions set state=1 where id=?',[id])
            if(res.affectedRows > 0){
                ctx.body = {
                    code: 200,
                    msg: '删除成功'
                }
            }
        } catch (error) {
            ctx.body = {
                code: 201,
                msg: '删除失败' + error
            }
        }
    },
    // 分页获取文题目列表
    list: async (ctx, next) => {
        const {startPage,endPage} =  await ctx.service.utils.paging(ctx)
        await ctx.db.query('select * from questions where state=0 ').then(async res=>{
            const total =  res[0].length
            await ctx.db.query('select * from questions where state=0  order by createTime desc limit ?,?',[startPage,endPage]).then(ress=>{
                ctx.body = {
                    code:200,
                    msg:'获取题目列表成功',
                    article:ress[0],
                    total:total
                }
            })
        })
    },
    // 获取题目详情
    detail:async (ctx, next) => {
        const {id} =  ctx.request.query
       try {
            let [res] =  await ctx.db.query('select * from questions where id=? ',[id])
            ctx.body = {
                code: 200,
                msg: '获取详情成功',
                article:res[0]
            }
       } catch (error) {
        ctx.body = {
            code: 201,
            msg: '获取题目失败' + error
        }
       }
    },
}